package xxbiao;

import java.util.Arrays;

public class one<E> implements jiekou<E> {
    private static int DEFAULT = 5;
    private int size = 0;
    private Object[] capacity = new Object[DEFAULT];

    private void Is_full() {
        if (size == capacity.length) {
            //扩容
            Object[] tmp = Arrays.copyOf(capacity,2*DEFAULT);
            DEFAULT*=2;
            capacity = tmp;
        }
    }

    @Override
    public void add(E data) {
        Is_full();
        capacity[size++] = data;
    }

    private void insert(int pos,E data) {
        Is_full();
        for (int i = size - 1; i >= pos; i--) {
            capacity[i + 1] = capacity[i];
        }
        capacity[pos] = data;
        size++;
    }
    @Override
    public void add(int pos, E data) throws pos_Illegal {
        try {
            if (pos < 0 || pos > size) {
                throw new pos_Illegal("插入的坐标无效");
            }
            insert(pos,data);
        } catch (pos_Illegal e) {
            System.out.println("坐标错误");
            throw new pos_Illegal("插入的坐标无效");
        }
    }

    @Override
    public boolean contains(E toFind) {
        for (int i = 0; i < size; i++) {
            if (toFind.equals(capacity[i])) {
                return true;
            }
        }
       return false;
    }

    @Override
    public int indexOf(E toFind) {//值对应下表
        for (int i = 0; i < size; i++) {
            if (toFind.equals(capacity[i])) {
               return i;
            }
        }
        return -1;
    }

    @Override
    public E get(int pos) throws pos_Illegal{
        try {
            if (pos < 0 || pos >= size) {
                throw new pos_Illegal("插入的坐标无效");
            }
        } catch (pos_Illegal pos_illegal) {
            throw new pos_Illegal("坐标错误");
        }
        return (E) capacity[pos];
    }

    private void set_v(int pos, E value) {
        Is_full();
        capacity[pos++] = value;
    }
    @Override
    public void set(int pos, E value) {
        try {
            if (pos < 0 || pos > size) {
                throw new pos_Illegal("插入的坐标无效");
            }
            set_v(pos,value);
        } catch (pos_Illegal e) {
            System.out.println("坐标错误");
            throw new pos_Illegal("插入的坐标无效");
        }
    }

    @Override
    public void remove(E toRemove) {
        //找到该元素
        boolean flag = true;
        for (int i = 0; i < size; i++) {
            if (toRemove.equals(capacity[i])) {//找到了
                for (int j = i; j < size - 1; j++) {
                    capacity[j] = capacity[j + 1];
                }
                size--;
                flag = false;
                break;
            }
        }
        if (flag) {
            System.out.println("没有这个元素");
        }
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void clear() {
        for (int i = 0; i < capacity.length; i++) {
            capacity[i] = null;
        }
        size = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < size; i++) {
            System.out.print(capacity[i] + " ");
        }
        System.out.println();
    }

    public int find(E tofind) {
        for (int i = 0; i < size; i++) {
            if (tofind.equals(capacity[i])) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public String toString() {
        return "one{" +
                "size=" + size +
                ", capacity=" + Arrays.toString(capacity) +
                '}';
    }
}
